package cn.gson.fengboot.controller;

import cn.gson.fengboot.aspect.MyLog;
import cn.gson.fengboot.common.WeatherReportByCity;
import cn.gson.fengboot.entity.IPEntity;
import cn.gson.fengboot.model.pojos.QempFrom;
import cn.gson.fengboot.model.pojos.QmenusEntity;
import cn.gson.fengboot.model.routervo.Router;
import cn.gson.fengboot.model.service.DloginService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.*;
import java.util.*;

@RestController
@RequestMapping("/come")
public class DloginController {
    @Autowired
    DloginService dloginService;

    @RequestMapping("/login")
    @MyLog(modul = "登录系统",desc = "登录成功")
    public String login(String phone, String pwd,HttpSession session) throws Exception {
        QempFrom login = dloginService.login(phone, pwd);
        //存储到session当中
        IPEntity ipEntity =null;
        /**/
        if(login!=null){
            if(login.getEmpStatu().equals("在职")){
                if(login.getEmpService().equals("启用")){
                    session.setAttribute("user",login);
                    session.setMaxInactiveInterval(3*60*60);//设置session有效时长
                    ipEntity = getAddressCity();
                    QempFrom emp11 = (QempFrom)session.getAttribute("user");

                    JSONObject object = new JSONObject();  //创建Json对象
                    object.put("city", ipEntity);         //设置Json对象的属性
                    object.put("user", login);

                    return object.toString();
                }else {
                    return "stop";
                }

            }else{
                return "out";
            }

        }else {
            return "error";
        }
    }



    public IPEntity getAddressCity(){
        String param = "version=v1";
        StringBuilder sb = new StringBuilder();
        InputStream is = null;
        BufferedReader br = null;
        PrintWriter out = null;
        IPEntity ipEntity = null;
        try {
            //接口地址
            String url = "https://tianqiapi.com/api?version=v6&appid=93383613&appsecret=3fgjZXqh";
            URL uri = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
            connection.setRequestMethod("POST");
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(10000);
            connection.setRequestProperty("accept", "*/*");
            //发送参数
            connection.setDoOutput(true);
            out = new PrintWriter(connection.getOutputStream());
            out.print(param);
            out.flush();
            //接收结果
            is = connection.getInputStream();
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line;
            //缓冲逐行读取
            while ( ( line = br.readLine() ) != null ) {
                sb.append(line);
            }
            System.out.println(sb.toString());

            JSONObject obj = (JSONObject)JSONObject.parse(sb.toString());
            System.out.println(obj.toString());
            String city=obj.getString("city");
            String date=obj.getString("date");
            String week=obj.getString("week");
            String wea =obj.getString("wea");
            String tem =obj.getString("tem");
            ipEntity = new IPEntity(city,date,week,wea,tem);

            System.out.println(ipEntity.toString());
            return ipEntity;
        } catch ( Exception ignored ) {
        } finally {
            //关闭流
            try {
                if(is!=null){
                    is.close();
                }
                if(br!=null){
                    br.close();
                }
                if (out!=null){
                    out.close();
                }
            } catch ( Exception ignored ) {}
        }

        return ipEntity;
    }


    /*获取员工的权限菜单*/
    /*获取员工按钮权限的标识符*/
    @RequestMapping("/fun")
    public Map<String,Object > fublist(HttpServletRequest request){
        QempFrom emp = (QempFrom)request.getSession().getAttribute("user");//取session
        String empname = emp.getEmpName();
        Map<String,Object> map = new HashMap<>();
        map.put("myfun",dloginService.fublist(empname));
        map.put("mybutton",dloginService.mybuttonrouter(emp.getEmpId()));
        return map;
    }

    /*清除session*/
    @RequestMapping("/romovesession")
    public void removeSessionAttributr(HttpServletRequest request){

        Enumeration em = request.getSession().getAttributeNames();  //得到session中所有的属性名
        while (em.hasMoreElements()) {
            request.getSession().removeAttribute(em.nextElement().toString()); //遍历删除session中的值
        }
    }
    /*从登录session获取员工姓名*/
    public String getbuspeople(HttpServletRequest request){
        QempFrom emp=(QempFrom)request.getSession().getAttribute("user");
        String busPeople = emp.getEmpName();
        //取session
        return busPeople;
    };



    /*从登录session获取员工ID*/
    public Integer getbuspeopleId(HttpServletRequest request){
        QempFrom emp=(QempFrom)request.getSession().getAttribute("user");
        Integer empId = emp.getEmpId();
        //取session
        return empId;
    };

    /*获取登录员工路由*/
    @RequestMapping("/myemprouter")
    public List<Router> myemprouter(String empName){
        return dloginService.myemprouter(empName);
    }

}
